<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdapterWebController;
use App\Models\Business\Business;
use App\Models\Company\Company;
use App\Models\Music\MusicCheck;
use App\Models\Music\MusicSp;
use App\Models\Music\PdfFile;
use App\Models\Music\Special;
use App\Models\Sp\Sp;
use App\Modules\Music\Music;
use App\Services\Music\Music\MusicLogic;
use Illuminate\Http\Request;
use Excel;


class MusicController extends AdapterWebController
{
    public function musicIndex(){
        //音乐统计
        $res = $this->appLayerExec(Music::class, 'statisticsMusic');
        return view('admin.music.music_index', ['music' => 1, 'musicIndex' => 1, 'statistics' => $res['res'], 'musicCenter' => 1]);
    }

    public function searchMusic(Request $request){
        $data = $request->all();
        $res = (new MusicLogic())->musicList($data);
        return $res['res'];
    }

    /**
     * 领取音乐
     */
    public function musicGetTask()
    {
        $res = $this->appLayerExec(Music::class, 'musicGetTask');
        return $res['res'];
    }

    //音乐列表
    public function musicList(Request $request,$type=0){
        $postData = $request->all();
        $postData['type'] = $type;
        $res = $this->appLayerExec(Music::class, 'musicList', $postData);
        //dd($res);
        $sp = Sp::where('status','=',1)->get();
        if(!empty($sp)){
            $sp = $sp->toArray();
        }
        $check_man = \App\Models\Music\Music::select('check_man')->distinct()->get();
        return view('admin.music.music_statistics',['music'=>1,'musicCenter'=>1,'musicStatistics'=>1,'list'=>$res['res'],'sp'=>$sp,'check_man'=>$check_man]);
    }

    //修改提示
    public function amend(Request $request,$pars = '',$id = 0){
        $data = $request->all();
        unset($data['_token']);
        if($id == 0){
            $id = $data['id'];
        }
        if($pars == ''){
            foreach ($data as $k=>$v){
                if($k!='id'){ $pars = $k; }
            }
        }
        $res = \App\Models\Music\Music::select($pars)->where('id', '=', $id)->first()->toArray();
        $value = $res[$pars];
        if($request->isMethod('get') && $pars!='lyrics' && $pars!='description'){
            return view('admin.music.amend',['name'=>$pars,'id'=>$id,'value'=>$value]);
        }else if($request->isMethod('get') && ($pars!='lyrics' || $pars!='description')){
            return view('admin.music.special_amend',['name'=>$pars,'id'=>$id,'value'=>$value]);
        }
        $id = \App\Models\Music\Music::where('id','=',$data['id'])->update($data);
        $res = \App\Models\Music\Music::select($pars)->where('id', '=', $id)->first()->toArray();
        $value = $res[$pars];
        if($id){
            flash('修改成功')->success();
        }else{
            flash('修改失败')->error();
        }
        if($pars == 'lyrics' || $pars == 'description'){
            return view('admin.music.special_amend',['name'=>$pars,'id'=>$id,'value'=>$value]);
        }else{
            return view('admin.music.amend',['name'=>$pars,'id'=>$id,'value'=>$value]);
        }
    }

    //标记打回
    public function sign(Request $request,$pars = '',$id = 0){
        $data = $request->all();
        unset($data['_toke']);
        if($id==0){$id = $data['id'];}
        $refuse = \App\Models\Music\Music::where('id','=',$id)->select('detail_refuse')->get()->toArray();
        $res = json_decode($refuse[0]['detail_refuse'],true);
        $info = '';
        if(!empty($res[$pars])){
            $info = $res[$pars];
        }
        if($request->isMethod('get')){
            return view('admin.music.sign',['name'=>$pars,'id'=>$id,'info'=>$info]);
        }
        foreach($data as $k=>$v){
            if($k!='id'){
                $pars = $k;
            }
        }
        $res[$pars] = $data[$pars];
        $detail_refuse = json_encode($res);
        $u = \App\Models\Music\Music::where('id','=',$id)->update(['detail_refuse'=>$detail_refuse]);
        if($u){
            flash('标记成功')->success();
        }else{
            flash('标记失败')->error();
        }
        return view('admin.music.sign',['name'=>$pars,'id'=>$id,'info'=>$info]);
    }

    //弹出层
    public function bigLay($pars,$id){
        $refuse = \App\Models\Music\Music::where('id','=',$id)->select($pars)->first()->toArray();
        return view('admin.music.big_lay',['info'=>$refuse[$pars],'type'=>$pars]);

    }

    //音乐详情
    public function musicDetail($id){
        $postData['id'] = $id;
        $res = $this->appLayerExec(Music::class, 'musicDetail', $postData);
        return $res['res'];
    }

    //修改sp
    public function updateSp(Request $request,$id){
        $postData = $request->all();
        $postData['id'] = $id;
        $this->appLayerExec(Sp::class, 'updateSp', $postData);
    }

    //审核音乐
    public function checkMusic(Request $request){
        $postData = array_filter($request->all());
        $res = $this->appLayerExec(Music::class,'checkMusic',$postData);
        return $res['res'];
    }

    //作品上线
    public function workOnline(Request $request){
        $postData = $request->all();
        $postData['type'] = 3;
        $res = $this->appLayerExec(Music::class, 'musicList', $postData);
        $sp = Sp::get();
        if(!empty($sp)){
            $sp = $sp->toArray();
        }
        return view('admin.music.work_online',['music'=>1,'musicCenter'=>1,'workOnline'=>1,'list'=>$res['res'],'sp'=>$sp]);
    }

    //独家发布
    public function musicToSingle(Request $request){
        $postData = $request->all();
        $res = $this->appLayerExec(Music::class, 'musicToSingle', $postData);
        return $res['res'];
    }

    //作品上线音乐查询
    public function workOnlineSearch(Request $request){
        $postData = $request->all();
        $postData['type'] = 3;
        $res = $this->appLayerExec(Music::class, 'musicList', $postData);
        return $res['res'];
//        $sp = Sp::get()->toArray();
//        return view('admin.music.work_online',['music'=>1,'musicCenter'=>1,'workOnline'=>1,'list'=>$res['res'],'sp'=>$sp]);
    }

    //特殊授权上线
    public function special(Request $request){
        if($request->isMethod('get')){
            $res = \App\Models\Music\Music::where('type_status','=',3)->where('check_status','=',2)->get();
            if(!empty($res)){$res = $res->toArray();}
            $buss = Business::select('id','company_name')->get();
            if(!empty($buss)){$buss = $buss->toArray();}
            return view('admin.music.special',['info'=>$res,'buss'=>$buss]);
        };
        $postData = $request->all();
        $uploadRes = $this->fileUpload($request, 'files_name');
        if (!$uploadRes['status']) {
            throw_exception('请上传协议备案');
        }
        $postData['files_name'] = $uploadRes['url'];
        unset($postData['_token']);
        $s = Special::create($postData);
        \App\Models\Music\Music::where('type_status','=',3)->where('check_status','=',2)->update(['special_id'=>$s->id,'check_status'=>4]);
        flash('操作成功')->overlay();
        return redirect()->back();
    }

    //删除选择的音乐
    public function deleteChoose($id){
        $res = \App\Models\Music\Music::where('id','=',$id)->update(['type_status'=>0]);
        if($res){
            return '200';
        }
        return '400';
    }

    //编辑选择的音乐
    public function editorChoose(Request $request,$id){
        if($request->isMethod('get')){
            $info = MusicCheck::where('music_id','=',$id)->get()->toArray();
            $sp = Sp::get();
            if(!empty($sp)){
                $sp = $sp->toArray();
            }
            return view('admin.music.editor_choose',['info'=>$info[0],'id'=>$id,'sp'=>$sp]);
        }
        $data = $request->all();
        $data['end_time'] = date("Y-m-d", strtotime("+" . $data['first_time'] . " day", strtotime($data['start_time'])));
        $id = $data['id'];
        unset($data['_token']);
        unset($data['id']);
        $res = MusicCheck::where('music_id','=',$id)->update($data);
        if($res){
            flash()->success('修改成功');
            return redirect()->back();
        }else{
            flash()->error('修改失败');
            return redirect()->back();
        }
    }

    //重申音乐
    public function reiterateMusic(){
        $query = \App\Models\Music\Music::where('check_status','=',3)->where('check_man','=',session('username'))->get();
        if(!empty($query)){
            $res = $query->toArray();
        }
        return $res;
    }

    //正在审核的音乐
    public function checkingMusic(){
        $query = \App\Models\Music\Music::where('check_status','=',1)->where('check_man','=',session('username'))->get();
        if(!empty($query)){
            $res = $query->toArray();
        }
        return $res;
    }


    //根据平台统计音乐
    public function musicBySp($type = 0, $library = ''){
        $postData['type'] = $type;
        $postData['library'] = $library;
        session(['library'=>$library]);
        $res = $this->appLayerExec(Music::class,'musicBySp', $postData);
        $company = Company::select('id','company_name')->get();
        if(!empty($company)){
            $company = $company->toArray();
        }
        return view('admin.music.music_by_sp',['list'=>$res['res'],'type'=>$type,'company'=>$company]);
    }

    //音乐发向平台生成对应材料
    public function musicToSp(Request $request){
        $postData = $request->all();
        $res = $this->appLayerExec(Music::class,'musicToSp', $postData);
        $signPdf = PdfFile::where('type','=',1)->orderBy('id','desc')->first();
        $pdf = PdfFile::where('type','=',0)->orderBy('id','desc')->first();
        return view('admin.music.music_to_sp',['type'=>$postData['type'],'signPdf'=>$signPdf['name'],'pdf'=>$pdf['name']]);
    }

    //发送邮件
    public function sendEmail($type ,$library = 1){
        $res = MusicLogic::sendMail($type,$library);
        if($res['code'] == 200){
            return view('admin.jump')->with(['url'=>URL.'/admin/musicIndex']);
        }else{
            echo '操作失败';
        }
    }

   //显示对应sp
    public function showSp($id){
         $a = MusicSp::with('sp')->where('music_id','=',$id)->get()->toArray();
         return view('admin.music.show_sp',['list'=>$a]);
    }

    //查看授权文件
    public function author($id){
        $a = \App\Models\Music\Music::with('music_file','accompaniment_file','mv_file','video_file','karaoke_file')->where('id','=',$id)->get()->toArray();
        return view('admin.music.author',['list'=>$a]);
    }

    //查看特殊授权
    public function showSpecial($id){
        $a = \App\Models\Music\Music::with('special')->where('id','=',$id)->get()->toArray();
        return view('admin.music.show_special',['list'=>$a]);
    }

    //导出音乐表格excel
    public function exportMusicList(){
        $postData['type'] = 0;
        $data = $this->appLayerExec(Music::class, 'musicList',$postData);
        $res = $data['res']->toArray();
        $cellData = [
            ['作品名', '专辑名', '表演者', '上线时间', '性质', '库', '作品级别', '审核编辑', '发布类型', '推荐歌曲'],
        ];
        foreach ($res['data'] as $k => $v) {
            $cell[] = [$v['title'],$v['album'],$v['performer'],$v['online_time'],$v['property'],$v['music_check']['library'],$v['music_check']['work_rate'],$v['check_man'],$v['type_status_text'],$v['music_check']['is_commond_text']];
        }
        $c = array_merge($cellData, $cell);
        Excel::create('音乐列表', function($excel) use($c) {
            $excel->sheet('musicList', function ($sheet) use($c) {
                $sheet->setWidth('A', 40);
                $sheet->fromArray($c, null, 'A1', false, false);
            });
        })->export('xlsx');
    }
}